package com.lonely.fx.tree_table_view;

import com.lonely.fx.tree_table_view.entity.Person;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-04 16:53
 * @author: ztkj-hzb
 * @description: treeTableView cellfactory 设置
 **/
public class TreeTableViewCellFactorySettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        Button button = new Button("修改name");


        TreeTableView<Person> treeTableView = new TreeTableView<>();

        //创建tree数据
        TreeItem<Person> root = new TreeItem<>(new Person("汇总", 0, 0.0, false));

        TreeItem<Person> treeItem_1 = new TreeItem<>(new Person("小天", 22, 0.25, true));
        TreeItem<Person> treeItem_2 = new TreeItem<>(new Person("小地", 24, 0.5, false));
        TreeItem<Person> treeItem_3 = new TreeItem<>(new Person("小玄", 26, 0.75, true));
        TreeItem<Person> treeItem_4 = new TreeItem<>(new Person("小黄", 28, 1.0, false));
        TreeItem<Person> treeItem_5 = new TreeItem<>(new Person("小宇", 30, 0.25, true));
        TreeItem<Person> treeItem_6 = new TreeItem<>(new Person("小宙", 32, 0.50, false));
        TreeItem<Person> treeItem_7 = new TreeItem<>(new Person("小洪", 34, 0.75, true));
        TreeItem<Person> treeItem_8 = new TreeItem<>(new Person("小荒", 36, 1.0, false));

        root.getChildren().addAll(treeItem_1, treeItem_2, treeItem_3, treeItem_4);
        treeItem_4.getChildren().addAll(treeItem_5, treeItem_6, treeItem_7, treeItem_8);


        //创建列数据
        TreeTableColumn<Person, String> nameCol = new TreeTableColumn<>("用户名");
        TreeTableColumn<Person, Integer> ageCol = new TreeTableColumn<>("年龄");
        TreeTableColumn<Person, Double> progressCol = new TreeTableColumn<>("学习进度");
        TreeTableColumn<Person, Boolean> passCol = new TreeTableColumn<>("是否合格");
        treeTableView.getColumns().addAll(nameCol, ageCol, progressCol, passCol);


        //数据绑定
        nameCol.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
        ageCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Integer>("age"));
        progressCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Double>("progress"));
        passCol.setCellValueFactory(new TreeItemPropertyValueFactory<Person, Boolean>("pass"));


        //设置root节点
        treeTableView.setRoot(root);
        root.setExpanded(true);

        //设置宽高
        treeTableView.setPrefSize(600, 400);

        //设置列宽自动均分
        treeTableView.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

        //设置可编辑
        treeTableView.setEditable(true);
        //设置名称编辑方式
        nameCol.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());
        ageCol.setCellFactory(ComboBoxTreeTableCell.forTreeTableColumn(10, 20, 30));
        //ageCol.setCellFactory(ChoiceBoxTreeTableCell.forTreeTableColumn(10, 20, 30));

        progressCol.setCellFactory(ProgressBarTreeTableCell.forTreeTableColumn());

        passCol.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(passCol));


        anchorPane.getChildren().addAll(button, treeTableView);
        AnchorPane.setTopAnchor(treeTableView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //因为实体的naem是普通数据类型，所以需要调用 refresh来更新
                root.getValue().setName("aa");
                //treeTableView.refresh();
            }
        });


        treeTableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Person>>() {
            @Override
            public void changed(ObservableValue<? extends TreeItem<Person>> observable, TreeItem<Person> oldValue, TreeItem<Person> newValue) {
                System.out.println(newValue.getValue());
            }
        });
    }
}
